﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XNA.BLEND.dom {
  internal class SDNAStruct {
    internal SDNAStruct( dynamic sdna, int type ) {
      this.SDNA = sdna;

      this.Type = type;
      this.TotalLength = 0;

      this.Fields = new Dictionary<int, SDNAField>();
    }

    internal void AddField( int fieldindex, SDNAField field ) {
      this.Fields.Add( fieldindex, field );

      Dictionary<int, SDNAName> names = this.SDNA.Names;
      Dictionary<int, SDNAType> types = this.SDNA.Types;

      if ( names[field.Name].IsPointer ) {
        this.TotalLength += (int)this.SDNA.Document.FileHeader.__PointerSize;
      }
      else if ( names[field.Name].IsArray ) {
        string[] indexes = names[field.Name].ArrayIndexes.Split( ',' );

        int addlength = types[field.Type].Length;

        foreach ( string index in indexes ) {
          addlength *= int.Parse( index );
        }
        this.TotalLength += addlength;

        names[field.Name].ArrayDimension = indexes.Length;
      }
      else {
        TotalLength += types[field.Type].Length;
      }
    }    

    internal int Type;
    internal int TotalLength;
    internal Dictionary<int, SDNAField> Fields;

    internal dynamic SDNA = null;
  }
}
